package com.example.ovulationperiodtracker.fragment;

import android.content.Intent;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.CompoundButton;
import android.widget.RatingBar;
import android.widget.Toast;

import com.example.ovulationperiodtracker.Allactivity.MainActivity;
import com.example.ovulationperiodtracker.Allactivity.MedicineActivity;
import com.example.ovulationperiodtracker.Allactivity.SexActivity;
import com.example.ovulationperiodtracker.databinding.FragmentCalendar2Binding;
import com.example.ovulationperiodtracker.utils.CalendarUtils;
import com.example.ovulationperiodtracker.utils.MyLog;
import com.example.ovulationperiodtracker.view.InputDialog;
import com.example.ovulationperiodtracker.view.MyInnerPainter;
import com.example.ovulationperiodtracker.view.NavigationView;
import com.example.ovulationperiodtracker.view.NoteUtils;
import com.example.ovulationperiodtracker.view.XinQingView;
import com.necer.base.BaseFragment;
import com.necer.enumeration.DateChangeBehavior;
import com.necer.listener.OnCalendarChangedListener;

import java.time.LocalDate;
import java.util.List;

public class CalendarFragment2 extends BaseFragment<FragmentCalendar2Binding> {


    private LocalDate localDate;
    private NoteUtils.DayNote dayNote;
    private MyInnerPainter myInnerPainter;


    @Override
    public FragmentCalendar2Binding createBinding(LayoutInflater inflater, ViewGroup container) {
        return FragmentCalendar2Binding.inflate(inflater);
    }

    @Override
    protected void onCreated() {

        //超过今天的日期，心情等禁止点击
        binding.flZhezhao.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
            }
        });

        binding.navigation.setOnClickRightListener(new NavigationView.OnClickRightListener() {
            @Override
            public void onClickRight() {
                binding.ncalendar.toToday();
            }
        });

        myInnerPainter = new MyInnerPainter(context, binding.ncalendar);
        binding.ncalendar.setCalendarPainter(myInnerPainter);
        binding.ncalendar.setOnCalendarChangedListener(new OnCalendarChangedListener() {
            @Override
            public void onCalendarChange(int year, int month, LocalDate date, DateChangeBehavior dateChangeBehavior) {
                localDate = date;
                binding.navigation.setTitle(year + "年" + month + "月");

                binding.flZhezhao.setVisibility(date.isAfter(LocalDate.now()) ? View.VISIBLE : View.GONE);

                binding.tvDate.setText(date.isEqual(LocalDate.now()) ? "今天" : date.toString());

                binding.tvShouyun.setText(MyInnerPainter.yiYunQi.contains(date) ? "受孕率高" : "受孕率低");

                resetData();
            }
        });


        //默认今天
        localDate = LocalDate.now();
        binding.llMark.setOnClickListener(v -> {

            if (LocalDate.now().isBefore(localDate)) {
                Toast.makeText(context, "不能在未来的时间添加记录", Toast.LENGTH_SHORT).show();
                return;
            }

            new InputDialog(context, "请输入备注", binding.tvMark.getText().toString())
                    .setOnClickYesNoListener(new InputDialog.OnClickYesNoListener() {
                        @Override
                        public void onClickYes(String content) {
                            binding.tvMark.setText(content);

                            dayNote.mark = content;
                            NoteUtils.saveDayNote(dayNote);
                        }
                    }).show();
        });


        binding.llTao.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent intent = new Intent(getActivity(), SexActivity.class);
                intent.putExtra("datekey", localDate);
                startActivity(intent);
            }
        });


        //心情
        binding.xinQingVIew.setOnXinQingListener(new XinQingView.OnXinQingListener() {
            @Override
            public void onClickXinQing(String content, int position) {
                dayNote.xinQing = position;
                NoteUtils.saveDayNote(dayNote);
            }
        });

        //经量
        binding.rbJingLiang.setOnRatingBarChangeListener(new RatingBar.OnRatingBarChangeListener() {
            @Override
            public void onRatingChanged(RatingBar ratingBar, float rating, boolean fromUser) {
                dayNote.jingLiang = (int) rating;
                NoteUtils.saveDayNote(dayNote);

            }
        });

        //头晕
        binding.rbTouYun.setOnRatingBarChangeListener(new RatingBar.OnRatingBarChangeListener() {
            @Override
            public void onRatingChanged(RatingBar ratingBar, float rating, boolean fromUser) {
                dayNote.touYun = (int) rating;
                NoteUtils.saveDayNote(dayNote);
            }
        });

        //头痛
        binding.rbTouTong.setOnRatingBarChangeListener(new RatingBar.OnRatingBarChangeListener() {
            @Override
            public void onRatingChanged(RatingBar ratingBar, float rating, boolean fromUser) {
                dayNote.touTong = (int) rating;
                NoteUtils.saveDayNote(dayNote);
            }
        });

        //体重
        binding.llTiZhong.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (LocalDate.now().isBefore(localDate)) {
                    Toast.makeText(context, "不能在未来的时间添加记录", Toast.LENGTH_SHORT).show();
                    return;
                }

                new InputDialog(context, "体重", binding.tvTiZhong.getText().toString())
                        .setOnClickYesNoListener(new InputDialog.OnClickYesNoListener() {
                            @Override
                            public void onClickYes(String content) {
                                float temp = 0;
                                try {
                                    temp = Float.parseFloat(content);
                                } catch (Exception e) {

                                }
                                if (temp == 0.0) {
                                    Toast.makeText(context, "请输入正确的体重数据", Toast.LENGTH_SHORT).show();
                                    return;
                                }

                                binding.tvTiZhong.setText(content);
                                dayNote.weight = content;
                                NoteUtils.saveDayNote(dayNote);
                            }
                        }).show();
            }
        });

        //体温
        binding.llTiWen.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {

                if (LocalDate.now().isBefore(localDate)) {
                    Toast.makeText(context, "不能在未来的时间添加记录", Toast.LENGTH_SHORT).show();
                    return;
                }


                new InputDialog(context, "体温", binding.tvTiWen.getText().toString())
                        .setOnClickYesNoListener(new InputDialog.OnClickYesNoListener() {
                            @Override
                            public void onClickYes(String content) {
                                float temp = 0;
                                try {
                                    temp = Float.parseFloat(content);
                                } catch (Exception e) {
                                }
                                if (temp == 0.0) {
                                    Toast.makeText(context, "请输入正确的体温数据", Toast.LENGTH_SHORT).show();
                                    return;
                                }
                                binding.tvTiWen.setText(content);
                                dayNote.temperature = content;
                                NoteUtils.saveDayNote(dayNote);
                            }
                        }).show();
            }
        });

        //药品
        binding.tvMedicines.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent intent = new Intent(getActivity(), MedicineActivity.class);
                intent.putExtra("datekey", localDate);
                startActivity(intent);
            }
        });

    }


    @Override
    public void onResume() {
        super.onResume();
        resetData();
    }

    public void resetData() {

        myInnerPainter.notifyCalendar();

        dayNote = NoteUtils.getDayNote(localDate);

        binding.StartSwitch.setOnCheckedChangeListener(null);
        binding.EndSwitch.setOnCheckedChangeListener(null);

        binding.StartSwitch.setChecked(dayNote.jingStart == 1);
        binding.EndSwitch.setChecked(dayNote.jingEnd == 1);
        binding.tvMark.setText(dayNote.mark);

        binding.sexSwitch.setOnCheckedChangeListener(null);
        binding.sexSwitch.setChecked(dayNote.haveSex == 1);
        binding.llTao.setVisibility(dayNote.haveSex == 1 ? View.VISIBLE : View.GONE);
        binding.tvTao.setText(dayNote.tao == 1 ? "有" : "无");
        binding.tvPartnerName.setText(dayNote.partnerName);
        binding.tvSexCount.setText(dayNote.sexCount + "");
        binding.tvGaoCount.setText(dayNote.gaoCount + "");

        binding.xinQingVIew.setXinQing(dayNote.xinQing);
        binding.rbJingLiang.setRating(dayNote.jingLiang);
        binding.rbTouYun.setRating(dayNote.touYun);
        binding.rbTouTong.setRating(dayNote.touTong);

        binding.switchMedicines.setOnCheckedChangeListener(null);
        binding.switchMedicines.setChecked(dayNote.haveMedicine == 1);
        binding.tvMedicines.setText(dayNote.medicines);
        binding.tvMedicines.setVisibility(dayNote.haveMedicine == 1 ? View.VISIBLE : View.GONE);
        binding.tvTiWen.setText(dayNote.temperature);
        binding.tvTiZhong.setText(dayNote.weight);


        binding.StartSwitch.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
            @Override
            public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {

                if (isChecked && LocalDate.now().isBefore(localDate)) {
                    Toast.makeText(context, "不能在未来的时间添加记录", Toast.LENGTH_SHORT).show();
                    binding.StartSwitch.setChecked(false);
                    return;
                }

                dayNote.jingStart = isChecked ? 1 : 0;
                NoteUtils.saveDayNote(dayNote);
                myInnerPainter.notifyCalendar();
            }
        });

        binding.EndSwitch.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
            @Override
            public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
                if (isChecked && LocalDate.now().isBefore(localDate)) {
                    Toast.makeText(context, "不能在未来的时间添加记录", Toast.LENGTH_SHORT).show();
                    binding.EndSwitch.setChecked(false);
                    return;
                }

                dayNote.jingEnd = isChecked ? 1 : 0;
                NoteUtils.saveDayNote(dayNote);
                myInnerPainter.notifyCalendar();
            }
        });

        binding.sexSwitch.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
            @Override
            public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {


                if (isChecked && LocalDate.now().isBefore(localDate)) {
                    Toast.makeText(context, "不能在未来的时间添加记录", Toast.LENGTH_SHORT).show();
                    binding.sexSwitch.setChecked(false);
                    return;
                }

                dayNote.haveSex = isChecked ? 1 : 0;
                NoteUtils.saveDayNote(dayNote);
                binding.llTao.setVisibility(dayNote.haveSex == 1 ? View.VISIBLE : View.GONE);
                if (isChecked) {
                    Intent intent = new Intent(getActivity(), SexActivity.class);
                    intent.putExtra("datekey", localDate);
                    startActivity(intent);
                }
            }
        });

        binding.switchMedicines.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
            @Override
            public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
                dayNote.haveMedicine = isChecked ? 1 : 0;
                binding.tvMedicines.setVisibility(dayNote.haveMedicine == 1 ? View.VISIBLE : View.GONE);
                NoteUtils.saveDayNote(dayNote);
                if (isChecked) {
                    Intent intent = new Intent(getActivity(), MedicineActivity.class);
                    intent.putExtra("datekey", localDate);
                    startActivity(intent);
                }
            }
        });
    }


}
